﻿# -*- coding: utf-8 -*-
import re
import sys
from django.utils import simplejson

'''
Структура кода субъекта
  84   000   000   000
  --   ---   ---   ---
  1ур. 2ур.  3ур.  4ур.
  
'''
MODEL_NAME = {}
MODEL_NAME['l1'] = 'geography.L1Object'
MODEL_NAME['l2'] = 'geography.L2Object'
MODEL_NAME['l34'] = 'geography.L34Object'

l1_dict = {}
l2_dict = {}
l34_dict = {}

l1_i = 0
l2_i = 0
l34_i = 0

input = open('input.cvs', 'r')

for line in input.readlines():
    record = line.split(',')
    for rec in record:
        rec = rec.rstrip('"').lstrip('"')
    code = record[2].rstrip('"').lstrip('"')
    match = re.match(r'^(?P<l1>\d{2})(?P<l2>\d{3})(?P<l3>\d{3})(?P<l4>\d+)$', code)
    if match is not None:
        if re.match(r'^000*$', match.group('l4')) is not None:
            if re.match(r'^000*$', match.group('l3')) is not None:
                if re.match(r'^000*$', match.group('l2')) is not None:
                    l1_i += 1
                    l1_dict[code] = {}
                    l1_dict[code]['name'] = unicode(record[0].rstrip('"').lstrip('"'), 'cp1251')
                    l1_dict[code]['abbr'] = unicode(record[1].rstrip('"').lstrip('"'), 'cp1251')
                    l1_dict[code]['code'] = match.group('l1')
                    l1_dict[code]['parent'] = 1
                else:
                    l2_i += 1
                    l2_dict[code] = {}
                    l2_dict[code]['name'] = unicode(record[0].rstrip('"').lstrip('"'), 'cp1251')
                    l2_dict[code]['abbr'] = unicode(record[1].rstrip('"').lstrip('"'), 'cp1251')
                    l2_dict[code]['parent'] = match.group('l1')
                    l2_dict[code]['code'] = match.group('l1')+match.group('l2')
            else:
                l34_i += 1
                l34_dict[code] = {}
                l34_dict[code]['name'] = unicode(record[0].rstrip('"').lstrip('"'), 'cp1251')
                l34_dict[code]['abbr'] = unicode(record[1].rstrip('"').lstrip('"'), 'cp1251')
                l34_dict[code]['l2_parent'] = match.group('l1')+match.group('l2')
                l34_dict[code]['l1_parent'] = match.group('l1')
        else:
            l34_i += 1
            l34_dict[code] = {}
            l34_dict[code]['name'] = unicode(record[0].rstrip('"').lstrip('"'), 'cp1251')
            l34_dict[code]['abbr'] = unicode(record[1].rstrip('"').lstrip('"'), 'cp1251')
            l34_dict[code]['l2_parent'] = match.group('l1')+match.group('l2')
            l34_dict[code]['l1_parent'] = match.group('l1')

input.close()

def serialize(dictionary, dict_name):
    objects = []
    i = 0
    kk = 0
    count = 0
    output = open('%s.json' % dict_name, 'w')
    for key, dic in dictionary.items():
        i += 1
        fields = {}
        fields = dic
        _object = {}
        _object['pk'] = i
        _object['fields'] = fields
        _object['model'] = MODEL_NAME[dict_name]
        objects.append(_object)
        if count > 2000:
            output.write("%s" % simplejson.dumps(objects, indent=4))
            output.close()
            output = open('%s_%d.json' % (dict_name, kk), 'w')
            count = 0
            kk += 1
            objects = []
        count += 1
    output.write("%s" % simplejson.dumps(objects, indent=4))
    output.close()
    
serialize(l1_dict, 'l1')
#serialize(l2_dict, 'l2')
#serialize(l34_dict, 'l34')